//
//  NomaiModel.swift
//
//  Created by eme on 2016/12/25
//  Copyright (c) . All rights reserved.
//

import Foundation
import SwiftyJSON
import ModelProtocol

class NomaiModel: ModelProtocol {
    
    // MARK: Declaration for string constants to be used to decode and also serialize.
    internal let kNomaiProductSpecsnapKey: String = "productSpecsnap"
    internal let kNomaiPdnameKey: String = "pdname"
    internal let kNomaiPdidKey: String = "pdid"
    
    
    // MARK: 属性
    var productSpecsnap: [ProductSpecsnapModel]
    var pdname: String
    var pdid: Int
    
    var totalHeight:CGFloat{
        get{
            var total:CGFloat = 0.0
            for item in self.productSpecsnap{
                total = total + item.rowHeight
            }
            return total + 30
        }
    }
    
    
    // MARK: 实现ModelProtocol 协议， 解析json数据
    required  init?(json: JSON) {
        productSpecsnap = []
        if let items = json[kNomaiProductSpecsnapKey].array {
            for item in items {
                if let productSpecsnapModel = ProductSpecsnapModel(json: item){
                    productSpecsnap.append(productSpecsnapModel)
                }
            }
        }
        pdname = json[kNomaiPdnameKey].stringValue
        pdid = json[kNomaiPdidKey].intValue
        
    }
    
    
    /**
     Generates description of the object in the form of a NSDictionary.
     - returns: A Key value pair containing all valid values in the object.
     */
    internal func dictionaryRepresentation() -> [String : AnyObject ] {
        
        var dictionary: [String : AnyObject ] = [ : ]
        if (productSpecsnap.count) > 0 {
            var temp: [AnyObject] = []
            for item in productSpecsnap {
                temp.append(item.dictionaryRepresentation() as AnyObject)
            }
            dictionary.updateValue(temp as AnyObject, forKey: kNomaiProductSpecsnapKey)
        }
        dictionary.updateValue(pdname as AnyObject, forKey: kNomaiPdnameKey)
        
        dictionary.updateValue(pdid as AnyObject, forKey: kNomaiPdidKey)
        
        return dictionary
    }
    
    
}
